﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Windows.Forms;

namespace WCFSelfTuoGuan
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //ServiceModule servic = new ServiceModule();
            //ServiceHost host = new ServiceHost(servic);
            ServiceHost host = new ServiceHost(typeof(ServiceModule));
            host.Open();
            {
                //使用编程的方式启用元数据交换行为
                ServiceMetadataBehavior metadataBehavior;
                metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if(metadataBehavior is null)
                {
                    metadataBehavior = new ServiceMetadataBehavior();
                    metadataBehavior.HttpGetEnabled = true;
                    host.Description.Behaviors.Add(metadataBehavior);
                }
            }
            {
                //编程的方式添加TCP MEX终结点
                //WCF没有为元数据交换终结点提供专门的绑定类型，因此需要创建定制绑定。
                BindingElement bindingElement = new TcpTransportBindingElement();
                CustomBinding customBinding = new CustomBinding(bindingElement);
                Uri tcpBassAdress = new Uri("net.tcp://localhost:9000/");
                ServiceHost serviceHost = new ServiceHost(typeof(ServiceModule), tcpBassAdress);
                ServiceMetadataBehavior metadataBehavior = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if(metadataBehavior is null)
                {
                    metadataBehavior = new ServiceMetadataBehavior();
                    serviceHost.Description.Behaviors.Add(metadataBehavior);
                }
                serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), customBinding, "MEX");
            }

            {
                //简化ServiceHost<T> 类
                //ServiceHost<ServiceModule> service = new 

            }
            Application.Run(new Form1());
            host.Close();
        }
    }
}
